﻿namespace SenenFernandez.Sensktop
{
    using SenenFernandez.Sensktop.Enum;
    using SenenFernandez.SmartExtensionMethods;
    using SenenFernandez.SmartExtensionMethods.Interactivity;
    using System;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;

    /// <summary>
    /// IPlugin interface.
    /// </summary>
    public interface IPlugin : IDisposable
    {
        /// <summary>
        /// Gets the Status of this plugin.
        /// </summary>
        ApplicationStatus PluginStatus { get; }

        /// <summary>
        /// Gets the Name of this plugin.
        /// </summary>
        string Name { get; }

        /// <summary>
        /// Gets the Description of this plugin.
        /// This will be displayed on the Tooltip UI control.
        /// </summary>
        string Description { get; }

        /// <summary>
        /// Gets the icon URI for item, linked to IconPath element in Plugin at XML menu configuration file.
        /// </summary>
        string IconPath { get; }

        /// <summary>
        /// Initializes the Plugin.
        /// </summary>
        void Initialize();

        /// <summary>
        /// On click command.
        /// </summary>
        ICommand ClickCommand { get; }
    }

    public abstract class PluginBase : NotificationObject, IPlugin
    {
        private ICommand clickCommand;

        /// <summary>
        /// Gets the Status of this plugin.
        /// </summary>
        [XmlIgnore]
        public ApplicationStatus PluginStatus { get; private set; }

        /// <summary>
        /// Gets the Name of this plugin.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets the Description of this plugin.
        /// This will be displayed on the Tooltip UI control.
        /// </summary>
        public virtual string Description { get; set; }

        /// <summary>
        /// Gets or sets icon URI for item, linked to IconPath element in Plugin at XML menu configuration file.
        /// </summary>
        public string IconPath { get; set; }

        /// <summary>
        /// Gets or sets icon URI for item, linked to IconPath element in Plugin at XML menu configuration file.
        /// Optional, can use target icon if no available
        /// </summary>
        public ImageSource IconSource
        {
            get
            {
                return IconPath.ToImageSource();
            }
        }

        /// <summary>
        /// On click command.
        /// </summary>
        [XmlIgnore]
        public ICommand ClickCommand
        {
            get { return clickCommand ?? (clickCommand = new DelegateCommand(OnClick)); }
        }

        /// <summary>
        /// Initializes a new instance of the PluginBase class.
        /// </summary>
        protected PluginBase()
        {

        }

        /// <summary>
        /// Initializes the Plugin.
        /// </summary>
        public virtual void Initialize()
        {
            IsInitialized = true;
        }

        /// <summary>
        /// On Click command.
        /// </summary>
        public virtual void OnClick()
        {

        }

        /// <summary>
        /// Sets the Status of this plugin.
        /// </summary>
        public void SetPluginStatus(ApplicationStatus value)
        {
            if (value != PluginStatus)
            {
                PluginStatus = value;
                RaisePropertyChanged(() => PluginStatus);
            }
        }

        /// <summary>
        /// Child Window.
        /// </summary>
        [XmlIgnore]
        protected Window ChildWindow { get; set; }

        /// <summary>
        /// IsInitialized.
        /// </summary>
        protected bool IsInitialized { get; private set; }
    }
}